<?php
namespace Base;
class TagConfig {
	public function checkTag($tag){
		$tags = array(
			'table' => array('tag_name'=>'table','tag_text'=>'',
				'attributes'=>array("cellpadding"=>'', "cellspacing"=>'', "border"=>'', 'style'=>'', "id"=>'', 'class'=>'', 'name'=>'', "align"=>'', "width"=>'')),
			'td' => array('tag_name'=>'td','tag_text'=>'',
				'attributes'=>array("id"=>'', 'name'=>'', "valign"=>'top', "align"=>'left', 'style'=>'', "id"=>'', 'class'=>'', "width"=>'')),
			'hidden' => array('tag_name'=>'input','tag_text'=>'',
				'attributes'=>array("id"=>'', 'name'=>'', 'value'=>'', 'type'=>'hidden')),
			'text' => array('tag_name'=>'input','tag_text'=>'',
				'attributes'=>array("id"=>'', 'name'=>'', 'value'=>'', 'type'=>'', 'class'=>'', 'style'=>'', 'onclick'=>'', "onkeyup"=>'', "onfocus"=>'', "onblur"=>'', "maxlength"=>'', "size"=>'')),
			'option' => array('tag_name'=>'option','tag_text'=>'','tag_child'=>true,'attributes'=>array('value'=>'')),
			'radio' => array('tag_name'=>'input','tag_text'=>'','tag_child'=>true,
				'attributes'=>array('name'=>'', 'style'=>'', 'class'=>'', 'onclick'=>'', 'type'=>'radio')),
			'checkbox' => array('tag_name'=>'input','tag_text'=>'','tag_child'=>true,
				'attributes'=>array('name'=>'', 'style'=>'', 'class'=>'', 'onclick'=>'', 'type'=>'checkbox')),
			'button' => array('tag_name'=>'input','tag_text'=>'',
				'attributes'=>array('name'=>'', 'value'=>'', 'type'=>'button', "id"=>'', 'onclick'=>'', 'class'=>'', 'style'=>'')),
			'submit' => array('tag_name'=>'input','tag_text'=>'',
				'attributes'=>array('name'=>'', 'value'=>'', 'type'=>'submt', "id"=>'', 'onclick'=>'', 'class'=>'', 'style'=>'')),
			'br' => array('tag_name'=>'br','tag_text'=>'','tag_child'=>true,'attributes'=>array()),
			'html'=>array('tag_name'=>'','tag_text'=>'','attributes'=>array()),
			'js'=>array('tag_name'=>'script','tag_text'=>'','attributes'=>array('language'=>'javascript','src'=>'')),
			'css'=>array('tag_name'=>'link','tag_text'=>'','attributes'=>array('type'=>'text/css','rel'=>'stylesheet')),
		);

		return $tags[$tag]?$tags[$tag]:array('tag_name'=>$tag,'tag_text'=>'','attributes'=>array());
	}
}
class TagNode {
	public $name='';
	public $tag_name='';
	public $tag_text='';
	public $config=array();
	public $attributes=array();
	public $elements=array();
	public $tag_parent;
	public function __set($name,$value) {
		$this->attributes[$name]  =   $value;
	}
	public function __get($name) {
		return isset($this->attributes[$name])?$this->attributes[$name]:null;
	}
	public function __isset($name) {
		return isset($this->attributes[$name]);
	}
	public function __unset($name) {
		unset($this->attributes[$name]);
	}
	function setAttributes($params) {
		if(!empty($params) && is_array($params))
		{
			$attributes =  array_merge($this->attributes,$params);
			$this->attributes=$attributes;
			unset($attributes);
		}
	}
	public function __construct($name='',$text='',$array=array())
	{
		if(!empty($name) and $this->config=TagConfig::checkTag($name)) {
			$this->name=$name;
			$this->tag_name = $this->config['tag_name'];
			$this->tag_text = $this->config['tag_text'];
			$this->setAttributes($this->config['attributes']);
			$this->setAttributes($array);
		}
		if(!empty($text)) {
			$this->tag_text   =  $text;
		}
	}
	public function clear() {
		$this->tag_name='';
		$this->tag_text='';
		$this->name='';
		$this->attributes= array();
		$this->elements = array();
	}
	public function __call($method,$args) {
		if($tag=TagConfig::checkTag($method)){
			// 连贯操作的实现
			$e=clone $this;
			$e->clear();
			$e->name=strtolower($method);
			$e->config=$tag;
			$e->tag_name=isset($tag['tag_name'])?$tag['tag_name']:$e->name;
			$e->setAttributes($tag['attributes']);
			if (is_array($args[0])){
				$e->setAttributes($args[0]);
				$e->tag_text=isset($args[1])?$args[1]:$tag['tag_text'];
			}else{
				$e->tag_text=isset($args[0])?$args[0]:$tag['tag_text'];
				if (is_array($args[1])) $e->setAttributes($args[1]);
			}
			if ($this->config['tag_child'] && $this->tag_parent){
				$e->tag_parent=&$this->tag_parent;
				$this->tag_parent->elements[]=$e;
			}else{
				$e->tag_parent=&$this;
				$this->elements[]=$e;
			}
			return $e;
		}else{
			return $this;
		}
	}
	function __toString()
	{
		if (empty($this->tag_name)) return $this->tag_text;
		$html='<'.$this->tag_name;
		foreach($this->attributes as $key => $value)
		{
			if($value!='') $html .= ' '. $key. '="'. $value. '"';
		}
		if ($this->tag_text=='' && (sizeof($this->elements)==0||$this->config['tag_child'])){
			$html.=' />';
		}else{
			$html.= '>';
			foreach($this->elements as $e)
			{
				$html .= $e->__toString();
			}
			$html .=$this->tag_text;
			$html .= '</'.$this->tag_name.'>';
		}
		return $html;
	}
}
class Element extends TagNode {
	public $label;
	public $required;
	/*	public $tips;//提示
        public $rule;//规则
        public $empty;//为空时提示
        public $warn;//警告
        public $pass;//通过时提示
    */
}
class Form extends TagNode {
	public function __construct($name,$action='',$mothod="post") {
		parent::__construct('form','',array(
			'name' => $name,
			'method' => $mothod,
			'action' => $action,
			'style' => "padding: 0; margin: 0;"));
	}
	public function addElements($params)
	{
		$paramSize = sizeof($params);
		for($i = 0; $i < $paramSize; ++$i)
			$this->attachElement($params[$i]);
	}

	/*addElement allows users to add a single form element by passing an array.*/
	public function add($type,$name,$label,$value="", $additionalParams="")
	{
		$params = array("label" => $label, 'name' => $name);
		if($eleType == "file")
			$this->attributes["enctype"] = "multipart/form-data";
		if(!empty($type))
			$params['type'] = $type;
		$params['value'] = $value;
		if(!empty($additionalParams) && is_array($additionalParams))
		{
			foreach($additionalParams as $key => $value)
				$params[$key] = $value;
		}
		$this->attachElement($params);
	}
}



?>
